package com.cx.thread;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class RunnableExcutorsPool {
    public static void main(String[] args) {
        // 创建支持定时线程池
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(2);
        for (int i = 0; i < 5; i++) {
            //创建任务
            Runnable runnable = new Runnable(){
                @Override
                public void run() {
                    System.out.println("当前执行的线程为:"+Thread.currentThread().getName());
                }
            };
            //任务添加到线程池，延迟2秒后才能从队列中出队
            executorService.schedule(runnable, 2, TimeUnit.SECONDS);
        }
        //关闭线程池
        executorService.shutdown();
        //如果没有中断就一直等待
        while(!executorService.isTerminated()){
        }
    }
}